package com.devo.icecream.data

import com.devo.icecream.utils.Time
import com.google.gson.annotations.SerializedName
import com.xuhao.didi.core.iocore.interfaces.IPulseSendable
import org.json.JSONObject
import java.io.Serializable
import java.nio.ByteBuffer
import java.nio.ByteOrder


data class OrderDto(
    @SerializedName("couponCode")
    val couponCode: String = "", // string
    @SerializedName("foodId")
    val foodId: Int = 0, // 0
    @SerializedName("ingredients")
    val ingredients: List<DIYIngredients> = listOf(),
    @SerializedName("isDiy")
    val isDiy: Int = 0, // 0
    @SerializedName("orderId")
    val orderId: String = "", // string
    @SerializedName("payMode")
    val payMode: String = "", // wxPay
) {
    data class DIYIngredients(
        @SerializedName("id")
        val id: Int = 0, // 0
        @SerializedName("repeat")
        val repeat: Int = 0 // 0
    ) : Serializable

}

data class FailureDto(
    val orderId: String,
    val type: Int
)

data class SocketEntity(
    /**
     * 设备MAC地址
     */
    val mac: String = "",
    /**
     * plc码 第一次连接必传
     */
    val plcCode: String = "",
    /**
     * gps定位信息
     */
    val gpsLocation: String = "",
    /**
     * 请求类型 枚举值:0
     */
    val command: Int = 0,

    val content: String = "",
    // 以下为服务器发送过来的数据
    /**
     * 设备连接唯一id
     */
    var machineKey: String = "",

    var message: String = "",

    /**
     * 数据类型
     */
    val type: String = "",
    val data: Any = "",
) : IPulseSendable {
    override fun parse(): ByteArray {

        val str = JSONObject().apply {
            put("mac", mac)
            put("plcCode", plcCode)
            put("gpsLocation", gpsLocation)
            put("command", command)
            put("machineKey", machineKey)
            put("content", content)
        }.toString()

        val byteArray = str.toByteArray()
        val allocate = ByteBuffer.allocate(4 + byteArray.size)
        allocate.order(ByteOrder.BIG_ENDIAN)
        allocate.putInt(byteArray.size)
        allocate.put(byteArray)

        return allocate.array()
    }
}

data class BusinessTimeDto(val start: String = "", val end: String = "") {

    fun toRange() = Time.parse(start, ":")..Time.parse(end, ":")
}

data class IngredientDto(
    @SerializedName("buckets")
    val buckets: RecipeBean? = null, // null
    @SerializedName("categoryList")
    val categoryList: List<CategoryBean> = listOf(),
    @SerializedName("id")
    val id: Int = 0, // 5
    @SerializedName("ingredients")
    val ingredients: List<IngredientsBean> = listOf(),
    @SerializedName("menuName")
    val menuName: String = "", // 夏季菜单
    @SerializedName("name")
    val name: String = "" // 杭州测试仓位001
) {

    data class CategoryBean(
        @SerializedName("chooseNum")
        val chooseNum: Int = 0, // 1
        @SerializedName("id")
        val id: Int = 0, // 11
        @SerializedName("isMust")
        val isMust: Int = 0, // 1
        @SerializedName("name")
        val name: String = "", // 蜂蜜
        @SerializedName("status")
        val status: Int? = null, // null
        @SerializedName("sort")
        val sort: Int = 0, // null

    ) {

        override fun equals(other: Any?): Boolean {
            if (this === other) return true
            if (javaClass != other?.javaClass) return false

            other as CategoryBean

            if (chooseNum != other.chooseNum) return false
            if (id != other.id) return false
            if (isMust != other.isMust) return false
            if (name != other.name) return false
            if (status != other.status) return false
            if (sort != other.sort) return false

            return true
        }

        override fun hashCode(): Int {
            var result = chooseNum
            result = 31 * result + id
            result = 31 * result + isMust
            result = 31 * result + name.hashCode()
            result = 31 * result + (status ?: 0)
            result = 31 * result + sort
            return result
        }

    }

}

data class CheckOrderDtoBean(
    @SerializedName("orderId")
    val orderId: String = "", // string
    @SerializedName("recipes")
    val recipes: List<RecipeDtoBean> = listOf()
) {
    data class RecipeDtoBean(
        @SerializedName("bucket")
        val bucket: String = "", // string
        @SerializedName("weight")
        val weight: Float = 0.0f // 0
    )
}